package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.DeviceDataDto;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.vo.DeviceDataPointVo;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.IDeviceDataService;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;

/**
 * 家属与老人关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-29
 */
@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService
{
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;
    @Autowired
    private FamilyMemberElderMapper familyMemberMapper;

    /**
     * 查询家属与老人关联
     * 
     * @param id 家属与老人关联主键
     * @return 家属与老人关联
     */
    @Override
    public FamilyMemberElder selectFamilyMemberElderById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询家属与老人关联列表
     * 
     * @param familyMemberElder 家属与老人关联
     * @return 家属与老人关联
     */
    @Override
    public List<FamilyMemberElder> selectFamilyMemberElderList(FamilyMemberElder familyMemberElder)
    {
        return familyMemberElderMapper.selectFamilyMemberElderList(familyMemberElder);
    }

    /**
     * 新增家属与老人关联
     * 
     * @param familyMemberElder 家属与老人关联
     * @return 结果
     */
    @Override
    public int insertFamilyMemberElder(FamilyMemberElder familyMemberElder)
    {
        return save(familyMemberElder) ? 1 : 0;
    }

    /**
     * 修改家属与老人关联
     * 
     * @param familyMemberElder 家属与老人关联
     * @return 结果
     */
    @Override
    public int updateFamilyMemberElder(FamilyMemberElder familyMemberElder)
    {
        return updateById(familyMemberElder) ? 1 : 0;
    }

    /**
     * 批量删除家属与老人关联
     * 
     * @param ids 需要删除的家属与老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除家属与老人关联信息
     * 
     * @param id 家属与老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 家人分页查询
     * @return
     */
    @Override
    public List<FamilyMemberElderVo> listByPage() {
        Long userId = UserThreadLocal.getUserId();
        return familyMemberMapper.listByPage(userId);
    }

    /**
     * 家人解绑
     *
     * @param id
     */

    @Override
    public void deleteById(Long id) {
        this.removeById(id);
    }

    @Autowired
    private IDeviceDataService deviceDataService;
    /**
     * 按天统计查询指标数据
     * 统计某个设备下，某个物模型id，在一天内每小时对应的平均值
     *
     * @param dto
     * @return
     */
    @Override
    public List<DeviceDataPointVo> queryDeviceDataListByDay(DeviceDataDto dto) {
        //转换时间格式
        LocalDateTime startTime = this.convertTimeFormat(dto.getStartTime());
        LocalDateTime endTime = this.convertTimeFormat(dto.getEndTime());
        return familyMemberElderMapper.queryDeviceDataListByDay(dto.getFunctionId(),dto.getIotId(),startTime,endTime);

//        List<DeviceData> deviceDataList = deviceDataService.lambdaQuery().eq(DeviceData::getFunctionId, dto.getFunctionId())
//                .eq(DeviceData::getIotId, dto.getIotId())
//                .between(DeviceData::getAlarmTime, startTime, endTime).list();
//        deviceDataList.stream().filter(data -> data.getDataValue() != null)
//                .collect()
//        return null;
    }

    /**
     * 1.6 按周统计查询指标数据
     * 统计某个设备下，某个物模型id，在几天内每天对应的平均值
     *
     * @param dto
     * @return
     */
    @Override
    public List<DeviceDataPointVo> queryDeviceDataListByWeek(DeviceDataDto dto) {
        //转换时间格式
        LocalDateTime startTime = this.convertTimeFormat(dto.getStartTime());
        LocalDateTime endTime = this.convertTimeFormat(dto.getEndTime());
        return familyMemberElderMapper.queryDeviceDataListByWeek(dto.getFunctionId(),dto.getIotId(),startTime,endTime);
    }

    /**
     * 转换时间格式
     * @param time
     * @return
     */
    private LocalDateTime convertTimeFormat(String time) {
        Long timestamp = Long.valueOf(time);
        LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp)
                .atZone(ZoneId.of("Asia/Shanghai"))
                .toLocalDateTime();
        return localDateTime;
//        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }


    @Override
    public List<DeviceData> pageQueryAlertData(DeviceDataDto dto) {
        //转换时间格式
        LocalDateTime startTime = this.convertTimeFormat(dto.getStartTime());
        LocalDateTime endTime = this.convertTimeFormat(dto.getEndTime());
        return deviceDataService.lambdaQuery().between(DeviceData::getAlarmTime,startTime,endTime)
                .eq(DeviceData::getIotId,dto.getIotId())
                .eq(DeviceData::getFunctionId,dto.getFunctionId())
                .list();
    }

    @Autowired
    private IElderService elderService;
    /**
     * 1.2 查询家人列表
     *
     * @return
     */
    @Override
    public List<FamilyMemberElder> mylist() {
        Long userId = UserThreadLocal.getUserId();
        List<FamilyMemberElder> list = this.lambdaQuery().eq(FamilyMemberElder::getFamilyMemberId, userId).list();
        for (FamilyMemberElder familyMemberElder : list) {
            Elder elder = elderService.lambdaQuery().eq(Elder::getId, familyMemberElder.getElderId()).one();
            familyMemberElder.setElderName(elder.getName());
        }
        return list;
    }
}
